package br.com.localfloripa.entities;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ResourceBundle;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name="template")
public class Template implements Serializable {
	
	private static final long serialVersionUID = 5824277738628995792L;
	private static final Logger log = LoggerFactory.getLogger(Template.class);
	
	
	@SequenceGenerator(name="Template_Seq",sequenceName="template_seq")
	@Id @GeneratedValue(generator="Template_Seq")
	private Long id;
	@Column(name="logo",nullable=true)
	private String logo;
	@Column(name="banner",nullable=true)
	private String banner;
	@Column(name="corretor",nullable=true)
	private String corretor;
	@Column(name="cor_fundo",nullable=false)
	private String corFundo;
	@Column(name="cor_barra",nullable=false)
	private String corBarra;
	@Column(name="cor_barra_menu",nullable=false)
	private String corBarraMenu;
	@Column(name="cor_fontes",nullable=false)
	private String corFontes;
	@Column(name="cor_fundo_menu_busca",nullable=false)
	private String corFundoMenuBusca;
	@Column(name="cor_fundo_rodape",nullable=false)
	private String corFundoRodape;
	@Column(name="cor_fundo_logo",nullable=false)
	private String corFundoLogo;
	@Column(name="cor_fundo_lancamento",nullable=false)
	private String corFundoLancamento;	
	@Column(name="cor_fundo_destaque",nullable=false)
	private String corFundoDestaque;	
	@Column(name="url",nullable=true)
	private String url;
	@Column(name="quem_somos",nullable=true)
	private String quemSomos;
	
	public Template() {
		/** Registra cores deafult **/
		this.corBarra = "255,255,255";
		this.corBarraMenu = "255,255,255";
		this.corFontes = "0,0,0";
		this.corFundo = "255,255,255";
		this.corFundoDestaque = "255,255,255";
		this.corFundoLancamento = "255,255,255";
		this.corFundoLogo = "255,255,255";
		this.corFundoMenuBusca = "255,255,255";
		this.corFundoRodape = "255,255,255";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLogo() {
		return logo;
	}

	public String getBanner() {
		return banner;
	}

	public void setBanner(String banner) {
		this.banner = banner;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getCorretor() {
		return corretor;
	}

	public void setCorretor(String corretor) {
		this.corretor = corretor;
	}

	public String getCorFundo() {
		return corFundo;
	}

	public void setCorFundo(String corFundo) {
		this.corFundo = corFundo;
	}

	public String getCorBarra() {
		return corBarra;
	}

	public void setCorBarra(String corBarra) {
		this.corBarra = corBarra;
	}

	public String getCorFontes() {
		return corFontes;
	}

	public void setCorFontes(String corFontes) {
		this.corFontes = corFontes;
	}

	public String getCorBarraMenu() {
		return corBarraMenu;
	}

	public void setCorBarraMenu(String corBarraMenu) {
		this.corBarraMenu = corBarraMenu;
	}

	public String getCorFundoMenuBusca() {
		return corFundoMenuBusca;
	}

	public void setCorFundoMenuBusca(String corFundoMenuBusca) {
		this.corFundoMenuBusca = corFundoMenuBusca;
	}
	
	public String getCorFundoRodape() {
		return corFundoRodape;
	}

	public void setCorFundoRodape(String corFundoRodape) {
		this.corFundoRodape = corFundoRodape;
	}

	public String getCorFundoLogo() {
		return corFundoLogo;
	}

	public void setCorFundoLogo(String corFundoLogo) {
		this.corFundoLogo = corFundoLogo;
	}

	public String getCorFundoLancamento() {
		return corFundoLancamento;
	}

	public void setCorFundoLancamento(String corFundoLancamento) {
		this.corFundoLancamento = corFundoLancamento;
	}

	public String getCorFundoDestaque() {
		return corFundoDestaque;
	}

	public void setCorFundoDestaque(String corFundoDestaque) {
		this.corFundoDestaque = corFundoDestaque;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getQuemSomos() {
		return quemSomos;
	}

	public void setQuemSomos(String quemSomos) {
		this.quemSomos = quemSomos;
	}

	@Transient
	public StreamedContent getMediaBanner() {
		
		StreamedContent media = null;
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageResources");
		File file = new File(bundle.getString("path.files.images.base")+ this.banner);
		
		try {
			InputStream stream = new FileInputStream(file);  
			media = new DefaultStreamedContent(stream, "application/x-shockwave-flash");
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(),e);
		}	    
		return media;
	}
	
	@Transient
	public String getPathLogo() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageResources");
		return bundle.getString("path.servlet.images.base")+"?photo="+this.logo+"&type="+TipoImagem.TEMPLATE.value();
	}
	
	@Transient
	public String getPathCorretor() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageResources");
		return bundle.getString("path.servlet.images.base")+"?photo="+this.corretor+"&type="+TipoImagem.LOGO.value();
	}
	
	@Transient
	public String getHtmlCorFundo() {
		
		String cor = "";
		if (corFundo != null) {
			String[] rgb = corFundo.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);			
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorBarra() {

		String cor = "";
		if (corBarra != null) {
			String[] rgb = corBarra.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);			
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorMenu() {
		String cor = "";
		if (corBarraMenu != null) {
			String[] rgb = corBarraMenu.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFundoMenu() {
		String cor = "";
		if (corFundoMenuBusca != null) {
			String[] rgb = corFundoMenuBusca.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFontes() {
		String cor = "";
		if (corFontes != null) {
			String[] rgb = corFontes.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFundoRodape() {
		String cor = "";
		if (corFundoRodape != null) {
			String[] rgb = corFundoRodape.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFundoLogo() {
		String cor = "";
		if (corFundoLogo != null) {
			String[] rgb = corFundoLogo.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFundoLancamento() {
		String cor = "";
		if (corFundoLancamento != null) {
			String[] rgb = corFundoLancamento.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlCorFundoDestaque() {
		String cor = "";
		if (corFundoDestaque != null) {
			String[] rgb = corFundoDestaque.split(",");
			Color rgbCor = new Color(Integer.parseInt(rgb[0].trim()),Integer.parseInt(rgb[1].trim()),Integer.parseInt(rgb[2].trim()));
			cor = "#"+Integer.toHexString(rgbCor.getRGB() & 0x00ffffff);
		}
		return cor;
	}
	
	@Transient
	public String getHtmlQuemSomos() {
		if (quemSomos != null)
			return quemSomos.replaceAll("\n", "<br/>");
		return null;
	}
	

}
